package com.xkdx.guangguang.fragment.shop.brandofshop;

import java.util.HashMap;
import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.shop.brandofshop.FloorsOfShopModule.SingleFloorInfo;
import com.xkdx.guangguang.module.network.AbsModule;

public class FloorsOfShopFragment extends BaseFragment {
	View view;
	Button back, brandClassification;
	ListView listView;
	public static List<SingleFloorInfo> list;

	FloorsOfShopAdapter adatper;

	FloorsOfShopAction floorsOfShopAction;
	FloorsOfShopModule floorOfShopModule;
	FloorsOfShopPresistence floorsOfShopPresistence;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.brand_of_shop_floor, container, false);
		findView();
		setOnClick();
		showLoading();

		return view;
	}

	@Override
	public void findView() {
		listView = (ListView) view.findViewById(R.id.floor_list);
		back = (Button) view.findViewById(R.id.classification_back);
		brandClassification = (Button) view.findViewById(R.id.classification);
		floorOfShopModule = new FloorsOfShopModule();
		floorsOfShopAction = new FloorsOfShopAction(getArguments().getString("shopID"));

	}

	@Override
	public void setOnClick() {
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getFragmentManager().popBackStack();

			}
		});
		brandClassification.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getFragmentManager().popBackStack();
			}
		});

	}

	@Override
	public void showLoading() {
		dataLoadDialog.show();
		floorsOfShopPresistence = new FloorsOfShopPresistence(this);
		((ManagerActivity) getActivity()).addCurrentTask(floorsOfShopPresistence);
		floorsOfShopPresistence.setActitons(floorsOfShopAction);
		floorsOfShopPresistence.setModule(floorOfShopModule);
		floorsOfShopPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (floorsOfShopPresistence != null) {
					floorsOfShopPresistence.cancel(true);
				}
				getFragmentManager().popBackStack();

			}
		});
		dataLoadDialog.show();

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			list=floorOfShopModule.list;
			setList();
		} else {
			refresh(map);
		}

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	private void setList() {
		adatper = new FloorsOfShopAdapter();
		listView.setAdapter(adatper);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				FragmentTransaction ft = getFragmentManager().beginTransaction();
				SingleFloorFragment singleFloorFragment = new SingleFloorFragment();
				Bundle b = new Bundle();
				b.putInt("position", position);
				singleFloorFragment.setArguments(b);
				ft.replace(R.id.fragment, singleFloorFragment);
				ft.addToBackStack(null);
				ft.commit();

			}
		});
	}

	class FloorsOfShopAdapter extends BaseAdapter {
		public FloorsOfShopAdapter() {
			// TODO Auto-generated constructor stub
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(getActivity()).inflate(R.layout.floor_item, null);
				holder = new ViewHolder();
				holder.floor = (TextView) convertView.findViewById(R.id.floor_name);
				holder.number = (TextView) convertView.findViewById(R.id.floor_brand_number);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.floor.setText(list.get(position).getFloorName());
			if (list.get(position).getList() != null) {
				holder.number.setText(list.get(position).getList().size() + "");
			}
			return convertView;
		}

		class ViewHolder {
			TextView floor;
			TextView number;
		}
	}

}
